package com.itheima.controller;

import com.itheima.aop.MyLog;
import com.itheima.entity.Emp;
import com.itheima.entity.PageQueryObject;
import com.itheima.entity.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @MyLog
    @GetMapping
    public Result serch(PageQueryObject pageQueryObject) {
        return Result.success(empService.serch(pageQueryObject));
    }

    @MyLog
    @PostMapping
//    这个注解不能省略啊！
    public Result save(@RequestBody Emp emp) {
        log.info("username = {}", emp.toString());
            empService.save(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.success(empService.getById(id));
    }

    @MyLog
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }

    @MyLog
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        empService.delete(ids);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        return Result.success(empService.list());
    }

}
